<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>简易手机通讯录</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 0;
        }

        form {
            background-color: white;
            border: 1px solid #ccc;
            padding: 20px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            width: 300px;
        }

        h2 {
            text-align: center;
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 5px;
        }

        input[type="text"] {
            width: 100%;
            padding: 8px;
            margin-bottom: 15px;
            border: 1px solid #ccc;
            border-radius: 3px;
        }

        button {
            padding: 8px 15px;
            border: 1px solid #ccc;
            border-radius: 3px;
            background-color: #f4f4f4;
            cursor: pointer;
            margin-right: 5px;
        }

        button:hover {
            background-color: #eaeaea;
        }

        textarea {
            width: 100%;
            height: 100px;
            margin-top: 10px;
            border: 1px solid #ccc;
            padding: 8px;
            overflow-y: auto;
        }

        footer {
            text-align: center;
            margin-top: 20px;
            color: #777;
        }
    </style>
</head>

<body>
    <form id="contactForm">
        <h2>手机通讯录管理器</h2>
        <label for="name">姓名：</label>
        <input type="text" id="name" required>
        <label for="phone">电话：</label>
        <input type="text" id="phone" required>
        <div style="display: flex; gap: 5px; margin-bottom: 10px;">
            <button type="button" id="addBtn">添加</button>
            <button type="button" id="queryBtn">查询</button>
            <button type="button" id="deleteBtn">删除</button>
            <button type="button" id="resetBtn">重置</button>
        </div>
        <textarea id="contactList" readonly></textarea>
        <footer>前端工作室设计 版权所有©</footer>
    </form>

    <script>
        const addBtn = document.getElementById('addBtn');
        const queryBtn = document.getElementById('queryBtn');
        const deleteBtn = document.getElementById('deleteBtn');
        const resetBtn = document.getElementById('resetBtn');
        const nameInput = document.getElementById('name');
        const phoneInput = document.getElementById('phone');
        const contactListTextarea = document.getElementById('contactList');

        // 从本地存储获取通讯录数据，若没有则初始化为空数组
        let contacts = JSON.parse(localStorage.getItem('contacts')) || [];

        // 渲染通讯录列表到文本域
        function renderContacts() {
            let list = '';
            contacts.forEach(contact => {
                list += `姓名: ${contact.name} --- 电话: ${contact.phone}\n`;
            });
            contactListTextarea.value = list;
        }

        // 添加联系人
        addBtn.addEventListener('click', () => {
            const name = nameInput.value;
            const phone = phoneInput.value;
            if (name && phone) {
                const newContact = { name, phone };
                contacts.push(newContact);
                localStorage.setItem('contacts', JSON.stringify(contacts));
                renderContacts();
                nameInput.value = '';
                phoneInput.value = '';
                // 弹出提示框显示添加成功
                alert('添加成功');
            }
        });

        // 查询联系人
        queryBtn.addEventListener('click', () => {
            const name = nameInput.value;
            const contact = contacts.find(c => c.name === name);
            if (contact) {
                phoneInput.value = contact.phone;
            } else {
                phoneInput.value = '';
            }
        });

        // 删除联系人
        deleteBtn.addEventListener('click', () => {
            const name = nameInput.value;
            contacts = contacts.filter(c => c.name!== name);
            localStorage.setItem('contacts', JSON.stringify(contacts));
            renderContacts();
            nameInput.value = '';
            phoneInput.value = '';
        });

        // 重置表单
        resetBtn.addEventListener('click', () => {
            nameInput.value = '';
            phoneInput.value = '';
        });

        // 初始化渲染
        renderContacts();
    </script>
</body>

</html>